
<template>
  <div class="register-list">
    <div class="header">
      <div class="label-title">
        <span class="title">选择时间：</span>
        <el-date-picker
          v-model="times"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </div>
      <div class="label-title">
        <span class="title">手机号码：</span>
        <el-input v-model="params.mobile" placeholder="请输入手机号码" />
      </div>
      <div class="label-title">
        <span class="title">检查人：</span>
        <el-input v-model="params.fullname" placeholder="请输入检查人" />
      </div>
      <div class="label-title">
        <span class="title">街道镇：</span>
        <el-select v-model="params.town_id" placeholder="请选择">
          <el-option v-for="item in town" :key="item.label" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <div class="label-title">
        <span class="title">角色：</span>
        <el-select v-model="params.role_id" placeholder="请选择">
          <el-option v-for="item in role" :key="item.label" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <div class="label-title">
        <span class="title">巡查条数大于：</span>
        <el-input v-model="params.sars_info_count_gq" placeholder="请填写巡查条数大于" />
      </div>
      <div class="label-title">
        <span class="title">巡查条数小于等于：</span>
        <el-input v-model="params.sars_info_count_leq" placeholder="请填写巡查条数小于等于" />
        <el-button type="primary" @click="search">筛选</el-button>
        <el-button type="primary" @click="download">导出</el-button>
      </div>
    </div>
    <div class="table">
      <el-table
        border
        :data="tableData"
        stripe
        style="width: 100%"
      >
        <el-table-column
          prop="customer_id"
          align="center"
          label="用户ID"
        />
        <el-table-column
          prop="mobile"
          align="center"
          label="注册手机"
        />
        <el-table-column
          prop="town_id"
          align="center"
          label="街道镇"
        >
          <template slot-scope="{row}">
            {{ oldTowns[row.town_id] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="grid_name"
          align="center"
          label="所属网格"
        />
        <el-table-column
          prop="fullname"
          align="center"
          label="检查人"
        />
        <el-table-column
          prop="role_id"
          align="center"
          label="角色"
        >
          <template slot-scope="{row}">
            {{ oldRoles[row.role_id] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="sars_info_count"
          align="center"
          label="巡查条数"
        />
        <el-table-column
          prop="status"
          align="center"
          label="状态"
        >
          <template slot-scope="{row}">
            <span v-if="row.status == 0">未激活</span>
            <span v-else-if="row.status == 1">正常</span>
            <span v-else>被禁用</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          align="center"
          label="注册时间"
        />
        <el-table-column
          prop="sars_info_count"
          align="center"
          label="操作"
        >
          <template slot-scope="{row}">
            <el-button type="text" @click="handleDetail(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>

          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    />
    <!-- 表单 -->
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="街道镇" :label-width="formLabelWidth">
          <el-select v-model="form.town_id" placeholder="请选择">
            <el-option v-for="item in town" :key="item.label" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属网格" :label-width="formLabelWidth">
          <el-input v-model="form.grid_name" autocomplete="off" placeholder="请选择所属网格" />
        </el-form-item>
        <el-form-item label="检查人" :label-width="formLabelWidth">
          <el-input v-model="form.fullname" autocomplete="off" placeholder="请输入检查人" />
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
          <el-select v-model="form.role_id" placeholder="请选择">
            <el-option v-for="item in role" :key="item.label" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { formConst, infoIndex, infoUpdate, infoDelete } from '@/api/user'
// import { getToken } from '@/utils/auth'
import { exportFile } from '@/utils/index'
export default {
  data() {
    return {
      form: {
        customer_id: '',
        status: '',
        town_id: '',
        grid_name: '',
        fullname: '',
        role_id: ''
      },
      formLabelWidth: '120px',
      dialogFormVisible: false,
      times: [],
      town: [],
      role: [],
      oldRoles: {},
      oldTowns: {},
      tableData: [],
      params: {
        start_at: '',
        end_at: '',
        mobile: '',
        fullname: '',
        town_id: '',
        role_id: '',
        sars_info_count_gq: '',
        sars_info_count_leq: '',
        per_page: 7,
        page: 1
      },
      total: 0
    }
  },
  async created() {
    const data = await formConst()
    const town = data.town
    const role = data.role
    this.oldRoles = JSON.parse(JSON.stringify(data.role))
    this.oldTowns = JSON.parse(JSON.stringify(data.town))

    console.log(data, 'data')

    for (const i in town) {
      this.town.push({
        label: town[i],
        value: i
      })
    }
    for (const i in role) {
      this.role.push({
        label: role[i],
        value: i
      })
    }
    this.init()
  },
  methods: {
    async init() {
      this.params.start_at = this.times[0] || ''
      this.params.end_at = this.times[1] || ''
      const { total, data } = await infoIndex(this.params)
      this.total = Number(total)
      this.tableData = data
    },
    search() {
      if (!this.times) {
        this.times = []
      }
      this.params.page = 1
      this.init()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.init()
    },
    handleDetail(row) {
      console.log(row, 'row')
      this.form.customer_id = row.customer_id
      this.form.status = row.status
      this.form.town_id = String(row.town_id)
      this.form.grid_name = row.grid_name
      this.form.fullname = row.fullname
      this.form.role_id = String(row.role_id)
      this.dialogFormVisible = true
    },
    handleDelete(row) {
      console.log(row, 'row')
      this.$confirm('删除后信息将不能恢复，确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await infoDelete(row.customer_id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.init()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async submit() {
      await infoUpdate(this.form)
      this.$message.success('修改成功')
      this.dialogFormVisible = false
      this.init()
    },
    // 导出
    download() {
      exportFile('v1/manage/customer/info/download', this.params)
    }
  }
}
</script>
<style lang="scss">
.register-list {
  padding: 20px;
  .header {
    display: flex;
    flex-wrap: wrap;
    .title {
      margin-left: 20px;
      display: inline-block;
      margin-top: 40px;
    }
    .el-input {
      width: 200px;
    }
    .el-button {
      margin-left: 20px;
    }
  }
  .table {
    padding: 20px;
  }
  .el-pagination {
    margin-bottom: 20px;
  }
}
</style>
